package com.ccb.sc.common.modules.member.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//psl//psl import com.ccb.sc.common.bind.exception.BizException;
import com.ccb.sc.common.code.BizCodeFace;
import com.ccb.sc.common.code.ErrorCode;
import com.ccb.sc.common.modules.member.entity.Member;
import com.ccb.sc.common.modules.member.entity.MemberPointsLog;
import com.ccb.sc.common.modules.member.enums.MemberPointsLogType;
import com.ccb.sc.common.modules.member.mapper.MemberPointsLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * @author ken
 * @version 1.0
 * @date 2022-07-31 21:24
 */
@Slf4j
@Service
public class MemberPointsService extends ServiceImpl<MemberPointsLogMapper, MemberPointsLog> {

    @Autowired
    private MemberService memberService;

    @Transactional(rollbackFor = Exception.class)
    public void award(Long memberId, int points, MemberPointsLogType logType, String associate) {
        if (points == 0) {
            log.warn("用户[{}]奖励积分数量为0，类型[{}]，关联id[{}]", memberId, logType, associate);
            return;
        }
        Member member = this.memberService.getById(memberId);
        Integer balance = member.getPoints();
        int memberPoints = Math.max(balance + points, 0);
        member.setPoints(memberPoints);
        member.setVersion(member.getVersion());
        boolean saveMember = this.memberService.updateById(member);
        if (!saveMember) {
            //psl  //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }

        MemberPointsLog memberPointsLog = new MemberPointsLog();
        memberPointsLog.setMemberId(memberId);
        memberPointsLog.setAmount(BigDecimal.valueOf(points));
        memberPointsLog.setBalance(BigDecimal.valueOf(balance));
        memberPointsLog.setChangeType(logType);
        memberPointsLog.setAssociate(associate);
        boolean saveLog = this.save(memberPointsLog);
        if (!saveLog) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL));
        }
    }

    public boolean isDone(Long memberId, MemberPointsLogType logType) {
        LocalDate now = LocalDate.now();
        return this.count(Wrappers.<MemberPointsLog>lambdaQuery()
                .eq(MemberPointsLog::getMemberId, memberId)
                .eq(MemberPointsLog::getChangeType, logType)
                .between(MemberPointsLog::getCreateTime, now, now.plusDays(1))) > 0;
    }

    public boolean countByAssociate(Long memberId, String associate) {
        return this.count(Wrappers.<MemberPointsLog>lambdaQuery()
                .eq(MemberPointsLog::getMemberId, memberId)
                .eq(MemberPointsLog::getAssociate, associate)) > 0;
    }
}
